<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Comment;
use Image;



class GoodscommentController extends Controller
{
    //评论展示
    public function goodscomment_list(){
        $arr=DB::table('goodscomment')->join('vip','vip.id','=','goodscomment.uid')->join('goodssize','goodssize.id','=','goodscomment.sid')->join('goods','goods.id','=','goodssize.gid')->select('goodscomment.id','username','goods.name','opinion','comment','imgs','goodscomment.state','ctime','reply')->orderBy('ctime','desc')->get();
        return view('/admin/goodscomment/goodscomment_list',['arr'=>$arr]);
    }

    // 评论详情
    public function goodscomment_details(Request $request){
        $piece=DB::table('goodscomment')->join('goodssize','goodssize.id','=','goodscomment.sid')->join('goods','goods.id','=','goodssize.gid')->where('goodscomment.id',$request->id)->select('comment','imgs','ctime','color','size')->first();
        $imgs=explode('&&',$piece->imgs);
        return view('/admin/goodscomment/goodscomment_details',['piece'=>$piece,'imgs'=>$imgs]);
    }

    // 评论回复
    public function goodscomment_reply(Request $request){
        if(DB::table('goodscomment')->where('id',$request->id)->update(['state'=>'1','reply'=>$request->reply])){
            return 1;
        }else{
            return 0;
        }
    }

    //删除评论
    public function del(Request $request){
        //获取他的id
        $id = $request->id;
        // dd($id);
        $stu = Comment::find($id);
        //获取原图的位置
        $img = 'admin/images/comment/'.$stu['img'];
        //获取缩放图的位置
        $simg = 'admin/images/comment/'.$stu['simg'];
        //删除表里的某一条
        $rowa = Comment::destroy($id);
        //判断图片的名子是否为空
        if(!empty($img)){
            unlink($img);
            unlink($simg);
            return '1';
        }else{
            return '0';
        }
    }

    //审核评论
    public function goodscomment_update(Request $request){
        // return $request->state;
        if($request->state){
            $state=DB::table('goodscomment')->where('id',$request->id)->value('state');
            // return $request->all();
            // return $data;
            if($state==0){
                DB::table('goodscomment')->where('id',$request->id)->update(['state'=>1]);
                return 1;
            }else{
                DB::table('goodscomment')->where('id',$request->id)->update(['state'=>0]);
                return 0;
            }
        }else{
            if(DB::table('goodscomment')->where('id',$request->id)->update($request->all())){
                return 1;
            }else{
                return 0;
            }
        }
    }


}
